package zzs.generalframework.console.core.parameter.commonparameter.dimissiontype.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import zzs.generalframework.common.core.parameter.commonparameter.dimissiontype.DimissionTypeBO;
import zzs.generalframework.common.core.parameter.commonparameter.dimissiontype.DimissionTypeInfo;
import zzs.generalframework.common.core.parameter.commonparameter.dimissiontype.DimissionTypePO;
import zzs.generalframework.console.common.exception.ConsoleException;
import zzs.generalframework.console.common.exception.ConsoleRollbackException;
import zzs.generalframework.console.common.model.ConsoleReturnBO;
import zzs.generalframework.console.common.model.ConsoleReturnBO.ConsoleResult;
import zzs.generalframework.console.core.parameter.commonparameter.dimissiontype.dao.IDimissionTypeDao;
import zzs.generalframework.console.core.parameter.commonparameter.dimissiontype.service.IDimissionTypeService;
import zzs.generalframework.util.core.reflect.BeanUtils;

/**
 * @title
 * @description
 * @project zzs-console
 * @package zzs.generalframework.console.core.parameter.commonparameter.dimissiontype.service.impl
 * @author zzs
 * @date 2018年2月2日
 * @version 1.0
 */
@Service("zzs.generalframework.console.core.parameter.commonparameter.dimissiontype.service.impl.dimissionTypeServiceImpl")
public class DimissionTypeServiceImpl implements IDimissionTypeService
{

	private Logger logger = LoggerFactory.getLogger(DimissionTypeServiceImpl.class);
	@Autowired
	BeanUtils beanUtils;
	@Autowired
	IDimissionTypeDao dimissionTypeDao;

	@Override
	public ConsoleReturnBO saveDimissionType(DimissionTypeBO dimissionTypeBO)
	{
		logger.debug("====保存离职类别信息开始====");
		logger.debug("====保存离职类别信息.入参:DimissionTypeBO=[{}]====", dimissionTypeBO);
		ConsoleReturnBO returnConsoleReturnBO = new ConsoleReturnBO();
		DimissionTypePO dimissionTypePO = new DimissionTypePO();
		copyDimissionTypeBO2DimissionTypePO(dimissionTypeBO, dimissionTypePO);
		Boolean insertResult = dimissionTypeDao.insert(dimissionTypePO);
		if (insertResult)
		{
			returnConsoleReturnBO.setResult(ConsoleResult.SUCCESS);
		}
		logger.debug("====保存离职类别信息.出参:ConsoleReturnBO=[{}]====", returnConsoleReturnBO);
		logger.info("====保存离职类别信息结束====");
		return returnConsoleReturnBO;
	}

	@Override
	public ConsoleReturnBO saveDimissionType(List<DimissionTypeBO> dimissionTypeBOList) throws ConsoleException
	{
		logger.info("====保存离职类别信息列表开始====");
		logger.debug("====保存离职类别信息列表.入参:DimissionTypeBOList=[{}]====", dimissionTypeBOList);
		ConsoleReturnBO returnConsoleReturnBO = new ConsoleReturnBO();
		returnConsoleReturnBO.setResult(ConsoleResult.SUCCESS);
		for (DimissionTypeBO itemBO : dimissionTypeBOList)
		{
			ConsoleReturnBO itemResult = saveDimissionType(itemBO);
			if (ConsoleResult.FAILD.equals(itemResult.getResult()))
			{
				returnConsoleReturnBO.setResult(ConsoleResult.FAILD);
				returnConsoleReturnBO.setMessage(itemResult.getMessage());
				throw new ConsoleRollbackException();
			}
		}
		logger.debug("====保存离职类别信息列表.出参:ConsoleReturnBO=[{}]====", returnConsoleReturnBO);
		logger.info("====保存离职类别信息列表结束====");
		return returnConsoleReturnBO;
	}

	@Override
	public ConsoleReturnBO getDimissionType(DimissionTypeInfo dimissionTypeInfo)
	{
		logger.info("====获取离职类别信息开始====");
		logger.debug("====获取离职类别信息.入参:DimissionTypeInfo=[{}]====", dimissionTypeInfo);
		ConsoleReturnBO returnConsoleReturnBO = new ConsoleReturnBO();
		List<DimissionTypePO> dimissionTypePOList = dimissionTypeDao.select(dimissionTypeInfo);
		if (dimissionTypePOList == null || dimissionTypePOList.size() == 0)
		{
			returnConsoleReturnBO.setMessage("未获取到记录");
		}
		else if (dimissionTypePOList.size() == 1)
		{
			returnConsoleReturnBO.setResult(ConsoleResult.SUCCESS);
			DimissionTypeBO dimissionTypeBO = new DimissionTypeBO();
			copyDimissionTypePO2DimissionTypeBO(dimissionTypePOList.get(0), dimissionTypeBO);
			returnConsoleReturnBO.setReturnObj(dimissionTypeBO);
		}
		else
		{
			returnConsoleReturnBO.setMessage("记录不唯一");
			DimissionTypeBO dimissionTypeBO = new DimissionTypeBO();
			copyDimissionTypePO2DimissionTypeBO(dimissionTypePOList.get(0), dimissionTypeBO);
			returnConsoleReturnBO.setReturnObj(dimissionTypeBO);
		}
		logger.debug("====获取离职类别信息.出参:ConsoleReturnBO=[{}]====", returnConsoleReturnBO);
		logger.info("====获取离职类别信息结束====");
		return returnConsoleReturnBO;
	}

	@Override
	public ConsoleReturnBO listDimissionType(DimissionTypeInfo dimissionTypeInfo)
	{
		logger.info("====获取离职类别信息列表开始====");
		logger.debug("====获取离职类别信息列表.入参:DimissionTypeInfo=[{}]====", dimissionTypeInfo);
		ConsoleReturnBO returnConsoleReturnBO = new ConsoleReturnBO();
		List<DimissionTypePO> dimissionTypePOList = dimissionTypeDao.select(dimissionTypeInfo);
		if (dimissionTypePOList == null || dimissionTypePOList.size() == 0)
		{
			returnConsoleReturnBO.setMessage("未获取到记录");
		}
		else
		{
			returnConsoleReturnBO.setResult(ConsoleResult.SUCCESS);
			List<DimissionTypeBO> resultList = new ArrayList<DimissionTypeBO>();
			for (DimissionTypePO itemPO : dimissionTypePOList)
			{
				DimissionTypeBO itemBO = new DimissionTypeBO();
				copyDimissionTypePO2DimissionTypeBO(itemPO, itemBO);
				resultList.add(itemBO);
			}
			returnConsoleReturnBO.setReturnObj(resultList);
		}
		logger.debug("====获取离职类别信息列表.出参:ConsoleReturnBO＝[{}]====", returnConsoleReturnBO);
		logger.info("====获取离职类别信息列表结束====");
		return returnConsoleReturnBO;
	}

	@Override
	public ConsoleReturnBO updateDimissionType(DimissionTypeBO dimissionTypeBO)
	{
		logger.info("====更新离职类别信息开始====");
		logger.debug("====更新离职类别信息.入参:DimissionTypeBO=[{}]====", dimissionTypeBO);
		ConsoleReturnBO returnConsoleReturnBO = new ConsoleReturnBO();
		DimissionTypePO dimissionTypePO = new DimissionTypePO();
		copyDimissionTypeBO2DimissionTypePO(dimissionTypeBO, dimissionTypePO);
		Boolean updateResult = dimissionTypeDao.update(dimissionTypePO);
		if (updateResult)
		{
			returnConsoleReturnBO.setResult(ConsoleResult.SUCCESS);
		}
		logger.debug("====更新离职类别信息.出参:ConsoleReturnBO=[{}]====", returnConsoleReturnBO);
		logger.info("====更新离职类别信息结束====");
		return returnConsoleReturnBO;
	}

	@Override
	public ConsoleReturnBO updateDimissionType(List<DimissionTypeBO> dimissionTypeBOList) throws ConsoleException
	{
		logger.info("====更新离职类别信息列表开始====");
		logger.debug("====更新离职类别信息列表.入参:List<DimissionTypeBO>=[{}]====", dimissionTypeBOList);
		ConsoleReturnBO returnConsoleReturnBO = new ConsoleReturnBO();
		returnConsoleReturnBO.setResult(ConsoleResult.SUCCESS);
		for (DimissionTypeBO itemBO : dimissionTypeBOList)
		{
			ConsoleReturnBO itemResult = updateDimissionType(itemBO);
			if (ConsoleResult.FAILD.equals(itemResult.getResult()))
			{
				returnConsoleReturnBO.setResult(ConsoleResult.FAILD);
				returnConsoleReturnBO.setMessage(itemResult.getMessage());
				throw new ConsoleRollbackException();
			}
		}
		logger.debug("====更新离职类别信息列表.出参:ConsoleReturnBO=[{}]====", returnConsoleReturnBO);
		logger.info("====更新离职类别信息列表结束====");
		return returnConsoleReturnBO;
	}

	@Override
	public ConsoleReturnBO removeDimissionType(DimissionTypeBO dimissionTypeBO)
	{
		logger.info("====删除离职类别信息开始====");
		logger.debug("====删除离职类别信息.入参:DimissionTypeBO＝[{}]======", dimissionTypeBO);
		ConsoleReturnBO returnConsoleReturnBO = new ConsoleReturnBO();
		DimissionTypePO dimissionTypePO = new DimissionTypePO();
		copyDimissionTypeBO2DimissionTypePO(dimissionTypeBO, dimissionTypePO);
		Boolean deleteResult = dimissionTypeDao.delete(dimissionTypePO);
		if (deleteResult)
		{
			returnConsoleReturnBO.setResult(ConsoleResult.SUCCESS);
		}
		logger.debug("====删除离职类别信息.出参:ConsoleReturnBO=[{}]====", returnConsoleReturnBO);
		logger.info("====删除离职类别信息结束====");
		return returnConsoleReturnBO;
	}

	@Override
	public ConsoleReturnBO removeDimissionType(List<DimissionTypeBO> dimissionTypeBOList) throws ConsoleException
	{
		logger.info("====删除离职类别信息列表开始====");
		logger.debug("====删除离职类别信息列表.入参:List<DimissionTypeBO>＝[{}]======", dimissionTypeBOList);
		ConsoleReturnBO returnConsoleReturnBO = new ConsoleReturnBO();
		returnConsoleReturnBO.setResult(ConsoleResult.SUCCESS);
		for (DimissionTypeBO itemBO : dimissionTypeBOList)
		{
			ConsoleReturnBO itemResult = removeDimissionType(itemBO);
			if (ConsoleResult.FAILD.equals(itemResult.getResult()))
			{
				returnConsoleReturnBO.setResult(ConsoleResult.FAILD);
				returnConsoleReturnBO.setMessage(itemResult.getMessage());
				throw new ConsoleRollbackException();
			}
		}
		logger.debug("====删除离职类别信息列表.出参:ConsoleReturnBO＝[{}]====", returnConsoleReturnBO);
		logger.info("====删除离职类别信息列表结束====");
		return returnConsoleReturnBO;
	}

	@Override
	public Long countDimissionType(DimissionTypeBO dimissionTypeBO)
	{
		logger.info("====统计离职类别信息开始====");
		logger.debug("====统计离职类别信息。入参：DimissionTypeBO＝[{}]======", dimissionTypeBO);
		Long returnLong = new Long(0);
		DimissionTypePO dimissionTypePO = new DimissionTypePO();
		copyDimissionTypeBO2DimissionTypePO(dimissionTypeBO, dimissionTypePO);
		returnLong = dimissionTypeDao.count(dimissionTypePO);
		logger.debug("====统计离职类别信息.出参:Long＝[{}]======", returnLong);
		logger.info("====统计离职类别信息结束====");
		return returnLong;
	}

	private void copyDimissionTypeBO2DimissionTypePO(DimissionTypeBO dimissionTypeBO, DimissionTypePO dimissionTypePO)
	{
		beanUtils.copyBeanNotNull(dimissionTypeBO, dimissionTypePO);
		if (dimissionTypeBO.getSysID() != null)
		{
			dimissionTypePO.setSeqID(dimissionTypeBO.getSysID());
		}
	}

	private void copyDimissionTypePO2DimissionTypeBO(DimissionTypePO dimissionTypePO, DimissionTypeBO dimissionTypeBO)
	{
		beanUtils.copyBeanNotNull(dimissionTypePO, dimissionTypeBO);
		if (dimissionTypePO.getSeqID() != null)
		{
			dimissionTypeBO.setSysID(dimissionTypePO.getSeqID());
		}
	}

}
